package com.code.demo.novate;

import android.app.Activity;

import com.code.demo.ui.common.DialogLoading;
import com.tamic.novate.Novate;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxStringCallback;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by android on 2018/4/26.
 */

public class NovateClient {

    public static String BASE_URL = "https://rest.qibuhz.com/";

    private Activity activity;
    private String mType;
    private Map<String, Object> parameters = new HashMap<String, Object>();
    private DialogLoading loading = null;
    private boolean silence = false;

    public NovateClient(Activity activity, String type) {
        this.activity = activity;
        this.mType = type;
    }

    public void post() {
        post(null);
    }

    public void post(boolean silence) {
        this.silence = silence;
        post(null);
    }

    public void post(Map<String, Object> map, boolean silence) {
        this.silence = silence;
        post(map);
    }

    private void filterEmpty() {
        if (parameters!=null && parameters.size()>0) {
            Iterator<Map.Entry<String, Object>> it = parameters.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry<String, Object> entry = it.next();
                if(entry.getValue() == null)
                    it.remove();//使用迭代器的remove()方法删除元素
            }
        }
    }

    public void post(Map<String, Object> map) {

        if (map != null) {
            parameters.putAll(map);
        }

        filterEmpty();

        new Novate.Builder(activity)
                .baseUrl(BASE_URL)
                .build()
                .rxPost(NovateUrl.getUrl(mType), parameters, new RxStringCallback() {

                    @Override
                    public void onStart(Object tag) {
                        super.onStart(tag);
                        if (loading == null && silence == false) {
                            loading = new DialogLoading(activity, "请稍候");
                            loading.show();
                        }
                    }

                    @Override
                    public void onNext(Object tag, String response) {
                        NovateResponse.dispatchPost(response, activity, mType);
                    }

                    @Override
                    public void onCancel(Object tag, Throwable e) {
                        if (loading != null) {
                            loading.dismiss();
                        }
                    }

                    @Override
                    public void onError(Object tag, Throwable e) {
                        if (loading != null) {
                            loading.dismiss();
                        }
                    }

                    @Override
                    public void onCompleted(Object tag) {
                        super.onCompleted(tag);
                        if (loading != null) {
                            loading.dismiss();
                        }
                    }
                });
    }

    public void get() {
        get(null);
    }

    public void get(Map<String, Object> map) {
        if (map != null) {
            parameters.putAll(map);
        }

        filterEmpty();

        new Novate.Builder(activity)
                .baseUrl(BASE_URL)
                .build()
                .rxGet(NovateUrl.getUrl(mType), parameters, new RxStringCallback() {
                    @Override
                    public void onNext(Object tag, String response) {
                        NovateResponse.dispatchGet(response, activity, mType);
                    }

                    @Override
                    public void onError(Object tag, Throwable e) {

                    }

                    @Override
                    public void onCancel(Object tag, Throwable e) {

                    }
                });
    }
}
